const api = require("../../../ws_micro_shop/utils/api.js");

const app = getApp();

Page({
  data: {
    qrcode: "",
    height: wx.systemInfo.windowHeight
  },
  onLoad(options) {
    let info = Number(wx.getStorageSync("user_info").info);
    app.tabbarindex = options.tabbarindex
      ? options.tabbarindex - 0
      : app.tabbarindex;
    app.pageOnLoad(this);
    let that = this;
    wx.showLoading({
      title: "正在加载",
      mask: true
    });
    app.ws_micro_shoprequest({
      url: api.share.get_qrcode_new,
      success: function(res) {
        if (res.code === 0) {
          wx.setStorageSync("qrcode", res.data);
          that.setData({
            qrcode: res.data
          });
          if (info === 0) {
            that.notice();
          }
        } else {
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false
          });
        }
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },
  onShow() {
    let user_info = wx.getStorageSync("user_info");
    this.setData({
      user_info
    });
  },
  saveGoodsQrcode() {
    let _this = this;
    if (!wx.saveImageToPhotosAlbum) {
      wx.showModal({
        title: "提示",
        content:
          "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。",
        showCancel: false
      });
      return;
    }
    wx.getSetting({
      success(res) {
        if (typeof res.authSetting["scope.writePhotosAlbum"] === "undefined") {
          _this.downLoadGoodsImg();
        } else if (res.authSetting["scope.writePhotosAlbum"] === false) {
          _this.setData({
            open_setting: true
          });
        } else {
          _this.downLoadGoodsImg();
        }
      }
    });
  },
  downLoadGoodsImg() {
    let _this = this;
    wx.downloadFile({
      url: _this.data.qrcode,
      success: function(e) {
        wx.showLoading({
          title: "正在下载推荐码",
          mask: true
        });
        wx.saveImageToPhotosAlbum({
          filePath: e.tempFilePath,
          success: function() {
            wx.showModal({
              title: "提示",
              content: "下载推荐码成功",
              showCancel: false
            });
          },
          fail: function(e) {
            wx.showModal({
              title: "提示",
              content: "下载推荐码失败",
              showCancel: false
            });
          },
          complete: function(e) {
            wx.hideLoading();
          }
        });
      },
      fail: function(e) {
        wx.showModal({
          title: "下载推荐码失败",
          content: e.errMsg + ";" + _this.data.qrcode,
          showCancel: false
        });
      },
      complete: function(e) {
        wx.hideLoading();
      }
    });
  },
  click() {
    wx.previewImage({
      current: this.data.qrcode,
      urls: [this.data.qrcode]
    });
  },
  notice() {
    wx.showModal({
      title: "温馨提示",
      content: "绑定个人信息可以让海报显示更佳哟",
      showCancel: false,
      confirmText: "知道了"
    });
  },
  // onShareAppMessage() {
  //   let isBrand = app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.isBrand;
  //   let user_info = wx.getStorageSync('user_info');
  //   let url = isBrand ? '/ws_micro_shop/pages/home/index?user_id=' : '/ws_micro_shop/pages/index/index?user_id=';
  //   let path = url + user_info.id + '&tabbarindex=' + app.tabbarindex + '&shopid=' + app.ws_micro_shop_globalData.shopid;
  //   return {
  //     path,
  //   };
  // },
});
